package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.hibernate.service.ServiceRegistry;


public class HibernateUtil {
    private static SessionFactory sessionFactory;

    static {
        Configuration cfg=new Configuration();
        cfg.configure();
        sessionFactory=cfg.buildSessionFactory();
        // 创建服务注册对象
//        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
//        // 创建会话工厂对象
//        sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
    }

    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }

    public static Session getSession(){
        return sessionFactory.openSession();
    }

    public static void main(String[] args) {
        Session session=getSession();
        Query query=session.createQuery("from User");
        System.out.println(query.list());
    }
}
